from apps.interface.client.baseViews import *
from apps.system.lessee.serializers import LesseeModelSerializer

class ClientViewSet(BaseClientViewSet):

    def get_queryset(self):
        params = getRequestParams(self.request)
        _list = ClientDao().findAll().select_related('lessee')
        if self.request.user.role.key == 'superAdmin' or self.request.user.role.key == 'admin':
            _list = _list
        return _list

    @action(methods=['post'], detail=False, authentication_classes=[], permission_classes=[])
    def findClientInfo(self, request):
        params = getRequestParams(self.request)
        clientname = params.get('clientname')
        if isEmptyStr(clientname):
            return Response(response_message('clientname字段必填'))
        _objs = ClientDao().findAll().filter(clientname=clientname)
        if not _objs.exists():
            return Response(response_message('记录不存在'))
        model = ClientModelSerializer(_objs.first()).data
        # model['lesseeModel'] = LesseeModelSerializer(_objs.first().lessee).data
        return Response(response_success(model))